package com.ui.basic.service.masterdata;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsecurity.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.basic.util.ComboBoxPopulator;
import com.common.basic.util.Globals;
import com.ui.basic.assembler.masterdata.BankAccAssembler;
import com.ui.basic.command.masterdata.CommandBankAcc;
import com.ui.basic.ibatis.dao.masterdata.TblMBankDAO;
import com.ui.basic.ibatis.dao.masterdata.TblMBankaccDAO;
import com.ui.basic.ibatis.dao.masterdata.TblMCountryDAO;
import com.ui.basic.ibatis.dao.masterdata.TblMCurrenciesDAO;
import com.ui.basic.ibatis.model.masterdata.TblMBankExample;
import com.ui.basic.ibatis.model.masterdata.TblMBankacc;
import com.ui.basic.ibatis.model.masterdata.TblMBankaccExample;
import com.ui.basic.ibatis.model.masterdata.TblMCountryExample;
import com.ui.basic.ibatis.model.masterdata.TblMCurrenciesExample;

@Service("bankAccService")
public class BankAccServiceImpl implements BankAccService {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory.getLog(BankAccServiceImpl.class);
	
	@Autowired
	private TblMBankDAO tblMBankDAO;
	@Autowired
	private TblMBankaccDAO tblMBankaccDAO;
	@Autowired
	private TblMCurrenciesDAO tblMCurrenciesDAO;
	@Autowired
	private TblMCountryDAO tblMCountryDAO;
	
	public void setTblMBankDAO(TblMBankDAO tblMBankDAO) {
		this.tblMBankDAO = tblMBankDAO;
	}
	public void setTblMBankaccDAO(TblMBankaccDAO tblMBankaccDAO) {
		this.tblMBankaccDAO = tblMBankaccDAO;
	}
	
	public void setTblMCurrenciesDAO(TblMCurrenciesDAO tblMCurrenciesDAO) {
		this.tblMCurrenciesDAO = tblMCurrenciesDAO;
	}
	public void setTblMCountryDAO(TblMCountryDAO tblMCountryDAO) {
		this.tblMCountryDAO = tblMCountryDAO;
	}
	
	@Transactional(readOnly = true)
	public CommandBankAcc getBankAcc(CommandBankAcc commandBankAcc)
			throws SQLException {
		LOGGER.debug(">>>BankAccServiceImpl.getBankAcc");
		if (commandBankAcc != null) {
			TblMBankaccExample example = new TblMBankaccExample();

			int rec = 0;
			try {
				rec = tblMBankaccDAO.countByExample(example);
			} catch (NullPointerException npe) {
				LOGGER.error(npe);
			}
			commandBankAcc.calculate(rec);
			example.setIntRowNum((commandBankAcc.getPgIndex() - 1)
					* commandBankAcc.getPgRows());
			example.setIntRowsPerPage(commandBankAcc.getPgRows());
			example.setOrderByClause("ACC_CODE");
			commandBankAcc.setData(tblMBankaccDAO.selectByExample(example));
		}
		return commandBankAcc;
	}
	
	@Transactional(readOnly = true)
	public void setCombo(CommandBankAcc commandBankAcc) throws SQLException {
		LOGGER.debug(">>>BankAccServiceImpl.setCombo");
		
		TblMCountryExample countryExample = new TblMCountryExample();
		countryExample.setOrderByClause("CODE");
		commandBankAcc.setCountryList(ComboBoxPopulator.populate(
				tblMCountryDAO.selectByExample(countryExample), "id",
				new String[] { "code", "name" }, "-"));
		
		TblMBankExample bankExample = new TblMBankExample();
		bankExample.setOrderByClause("CODE");
		commandBankAcc.setBankList(ComboBoxPopulator.populate(
				tblMBankDAO.selectByExample(bankExample), "id",
				new String[] { "code", "name" }, "-"));
		
		TblMCurrenciesExample currenciesExample = new TblMCurrenciesExample();
		currenciesExample.setOrderByClause("currabrev");
		commandBankAcc.setCurrencyList(ComboBoxPopulator.populate(
				tblMCurrenciesDAO.selectByExample(currenciesExample), "id",
				new String[] { "currabrev", "currency" }, "-"));
	}
	public CommandBankAcc getBankAccDetail(CommandBankAcc commandBankAcc)
			throws SQLException {
		LOGGER.debug(">>>BankAccServiceImpl.getBankAccDetail");
		commandBankAcc.setProperty(tblMBankaccDAO.selectByPrimaryKey(commandBankAcc.getId()));
		return commandBankAcc;
	}
	
	public void save(CommandBankAcc commandBankAcc) throws SQLException {
		LOGGER.debug(">>>BankAccServiceImpl.save");
		BankAccAssembler assembler = new BankAccAssembler();
		TblMBankacc bankAcc = assembler.toEntity(commandBankAcc);

		if(commandBankAcc.getId() != null){
			bankAcc.setChangedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			bankAcc.setChangeddate(new Date());
			try {
				tblMBankaccDAO.updateByPrimaryKeySelective(bankAcc);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}else{
			bankAcc.setCreatedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			bankAcc.setCreateddate(new Date());
			try {
				tblMBankaccDAO.insert(bankAcc);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}
	}
	public void delete(CommandBankAcc commandBankAcc) throws SQLException {
		LOGGER.debug(">>>BankAccServiceImpl.delete");
		TblMBankaccExample example = new TblMBankaccExample();
		
		if(commandBankAcc.getId()!=null){
			example.createCriteria().andIdEqualTo(commandBankAcc.getId());
		}else if(commandBankAcc.getSelectedRecord()!=null && commandBankAcc.getSelectedRecord().length>0){
			List<Long> idList = new ArrayList<Long>();
			for(String strId: commandBankAcc.getSelectedRecord())
				idList.add(Long.valueOf(strId));
			example.createCriteria().andIdIn(idList);
		}
		
		tblMBankaccDAO.deleteByExample(example);	
	}

}
